package com.iking.housekeeping.controller.servicetype;

import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.iking.housekeeping.common.CodeEnum;
import com.iking.housekeeping.common.PageParam;
import com.iking.housekeeping.common.PageResult;
import com.iking.housekeeping.common.ReturnCode;
import com.iking.housekeeping.entity.elderly.Elderly;
import com.iking.housekeeping.entity.servicestaff.ServiceStaff;
import com.iking.housekeeping.entity.servicetype.ServiceType;
import com.iking.housekeeping.httpbean.servicetype.reqbean.ServiceTypeIdList;
import com.iking.housekeeping.service.elderly.ElderlyService;
import com.iking.housekeeping.service.servicestaff.ServiceStaffService;
import com.iking.housekeeping.service.servicetype.ServiceTypeService;

/**
 * 服务类型信息
 * 
 * @author: gf
 * @date: 2018年4月18日 下午7:35:25
 */
@RestController
@RequestMapping("/serviceType")
public class ServiceTypeControler {

	/**
	 * 服务类型信息
	 */
	@Autowired
	private ServiceTypeService serviceTypeService;

	/**
	 * 服务人员信息
	 */
	@Autowired
	private ServiceStaffService serviceStaffService;

	/**
	 * 老人管理
	 */
	@Autowired
	private ElderlyService elderlyService;

	/**
	 * 添加服务类型信息
	 * 
	 * @param req
	 * @param bindingResult
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/insert")
	public ReturnCode insert(@RequestBody @Valid ServiceType req, BindingResult bindingResult) {
		// 验证参数
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL, msg, null);
		}
		// 验证类型名称不带空格
		if (req.getfName().contains(" ")) {
			return new ReturnCode(CodeEnum.FAIL, "类型名称不能带有空格！", null);
		}
		// 验证服务类型是否已存在
		ServiceType serviceType = new ServiceType();
		serviceType.setfName(req.getfName());
		if (serviceTypeService.selectRepeat(serviceType) != null) {
			return new ReturnCode(CodeEnum.FAIL, "类型名称已存在！", null);
		}
		// 执行方法
		serviceTypeService.insert(req);
		return new ReturnCode(CodeEnum.SUCCESS, req);
	}

	/**
	 * 删除服务信息类型
	 * 
	 * @param fId
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/delete")
	public ReturnCode delete(@RequestBody ServiceTypeIdList serviceTypeIdList) {
		// 根据服务类型查询服务人员信息
		List<ServiceStaff> serviceStaff = serviceStaffService.selectStaff(serviceTypeIdList.getfIds());
		if (serviceStaff.size() > 0) {
			return new ReturnCode(CodeEnum.FAIL, "请先删除该类型下的服务人员与老人", null);
		}
		for (String fId : serviceTypeIdList.getfIds()) {
			Elderly elderly = elderlyService.selectElderlyByFId(fId);
			if (elderly != null) {
				return new ReturnCode(CodeEnum.FAIL, "请先删除该类型下的服务人员与老人", null);
			}
		}
		// 执行方法
		int result = serviceTypeService.delete(serviceTypeIdList.getfIds());
		if (result < 1) {
			return new ReturnCode(CodeEnum.FAIL, "未找到要删除的数据", null);
		}
		return new ReturnCode(CodeEnum.SUCCESS);
	}

	/**
	 * 更新服务信息类型
	 * 
	 * @param req
	 * @param bindingResult
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/update")
	public ReturnCode update(@RequestBody @Valid ServiceType req, BindingResult bindingResult) {
		// 验证参数
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL, msg, null);
		}
		// 验证类型名称不带空格
		if ("".equals(req.getfName().trim())) {
			return new ReturnCode(CodeEnum.FAIL, "类型名称不能带有空格！");
		}
		// 验证服务类型是否已存在
		ServiceType serviceType = new ServiceType();
		serviceType.setfName(req.getfName());
		ServiceType serviceType1 = serviceTypeService.selectRepeat(serviceType);
		if (serviceType1 != null && !serviceType1.getfId().equals(req.getfId())) {
			return new ReturnCode(CodeEnum.FAIL, "类型名称已存在", null);
		}
		// 执行方法
		int result = serviceTypeService.update(req);
		if (result < 1) {
			return new ReturnCode(CodeEnum.FAIL, "更新服务人员信息失败", null);
		}
		return new ReturnCode(CodeEnum.SUCCESS, req);
	}
	/**
	 * 查询所有
	 * 
	 * @return ReturnCode
	 * @author gf
	 */
	@PostMapping("/selectAll")
	public ReturnCode selectAll(@RequestBody PageParam pageParam, BindingResult bindingResult) {
		// 查询
		Page<ServiceType> page = serviceTypeService.selectByPage(pageParam);
		if (page.getResult().size() < 1) {
			return new ReturnCode(CodeEnum.NOT_DATA);
		}
		// 返回分页到前台
		return new ReturnCode(CodeEnum.SUCCESS, new PageResult<ServiceType>(page));
	}

	/**
	 * 服务内容列表
	 * 
	 * @return ReturnCode
	 * @author gf
	 */
	// @PostMapping("/selectServiceMenu")
	// public ReturnCode selectServiceMenu() {
	// return new ReturnCode(CodeEnum.SUCCESS,
	// serviceTypeService.selectServiceMenu());
	// }
}
